package org.paul.tourism.api

import android.util.Log
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Headers
import retrofit2.http.POST
import retrofit2.http.Query
import java.util.concurrent.TimeUnit

/**
 *
 *
 *  created by paul at 2019-03-26
 *
 */

interface ApiClient {
    companion object {
        private const val BASE_URL = "http://api.isoftstone.com/iSSC/v3api/v3/"
        fun create(): ApiClient =
            create(HttpUrl.parse(BASE_URL)!!)
        private fun create(httpUrl: HttpUrl): ApiClient {
            val logger = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger {
                Log.d("ApiClient", it)
            })
            logger.level = HttpLoggingInterceptor.Level.BASIC

            val client = OkHttpClient.Builder()
                .addInterceptor(logger)
                .connectTimeout(20,TimeUnit.SECONDS)
                .readTimeout(20,TimeUnit.SECONDS)
                .build()
            return Retrofit.Builder()
                .baseUrl(httpUrl)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(ApiClient::class.java)
        }
    }

    @POST("token")
    @Headers(
        "Host:api.isoftstone.com",
        "Content-Length:0",
        "Pragma:no-cache",
        "Cache-Control:no-cache",
        "Accept:*/*",
        "Origin:http://i.isoftstone.com",
        "User-Agent:Mozilla/5.0 (Linux; Android 8.1.0; Redmi Note 5 Build/OPM1.171019.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/72.0.3626.121 Mobile Safari/537.36;iPSAClient",
        "Content-Type:application/json",
        "Referer:http://i.isoftstone.com/hwkq/kaoqin/huaweiv3.html",
        "Accept-Encoding:gzip, deflate",
        "Accept-Language:zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
        "X-Requested-With:com.isoftstone.client.ipsa",
        "Connection:keep-alive"
    )
    fun <T> bearer(@Query("t") t: Long): Call<T>

}